<?php

namespace App\Console\Commands;

use App\Models\Area;
use Illuminate\Console\Command;
use App\Common\Common;
use Exception;
use App\Jobs\SyncSendSms;

class ImportArea extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:area';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $xmlFilePath = public_path().'/area.csv';
        $handle = @fopen($xmlFilePath, "r");
        $header = true;
        if ($handle) {
            while (($buffer = fgets($handle, 4096)) !== false) {
                if($header){
                    $header = false;
                    continue;
                }
                $items = explode(",",$buffer);
                Area::create([
                    'code'=>$items[0] ,
                    'name'=>$items[1] ,
                    'level'=>$items[2] ,
                    'pcode'=>$items[3] ,
                    'create_at'=>$items[4] ,
                ]);
            }
            if (!feof($handle)) {
                echo "Error: unexpected fgets() fail\n";
            }
            fclose($handle);
        }
    }
}
